
const constans = require('../../constans/constans.js');
const request = require('../../request.js');
const Toast = require('../../utils/Toast.js');
const LogUtils = require('../../utils/LogUtils.js');
const util = require('../../utils/util.js')
const app = getApp();
var num = 0;
var num_ci = 0;
var num_ju = 1;
Page({
  data: {
    logs: [],
    height1: '',
    toGan:'0',
    max:[1,2,3,4,5,6,7,8,9],
    tyle:{
      data:{
        data2: [
          { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none',"a3": 'transform: rotateY(-180deg) translateZ(0);'},
          { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);'},
          { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);'},
          { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);'},
          { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);'},
          { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);'},
          { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);'},
          { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);'},
          { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);'},
        ]
      }
      
    }
      
    ,
     cai: 0,
     dip1:0,
    dip2: 0,
    dis_d:'none',
    dis1_d: 'none',
    dis2_d:'none',
     dip3: 0,
     img_user:'',
     tip:[],
    dip_bg:'none',//判断用户点
  },
   
  onLoad: function () {

    num = 0;
    this.setData({
      tyle: {

        data: {
          data2: [
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);'},
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);' },
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);' },
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);' },
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);' },
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);'},
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);'},
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);'},
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);' },
          ]
        }

      }
    });

  
    var id ='2iI4FZVvA26lnxpz8Fcl5n9fJ1ajIQ2IJXGt';

    app.requestHeader = { 'Authorization': wx.getStorageSync('Authorization') }
    console.log('用户' + wx.getStorageSync('Authorization'));
    console.log('packageId' + wx.getStorageSync('packageId'));
    console.log('channel' + wx.getStorageSync('channel'));
    console.log(app.requestHeader)





    let that = this;
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        // 获取可使用窗口宽度
        let clientHeight = res.windowHeight;
        // 获取可使用窗口高度
        let clientWidth = res.windowWidth;
        // 算出比例
        let ratio = 750 / clientWidth;
        // 算出高度(单位rpx)
        let height = clientHeight * ratio;
        // 设置高度
        that.setData({
          height1: height
        });
      }
    });
    var randomNum1 = Math.random()*9;
    var randomNum2 = Math.floor(randomNum1);
    console.log(randomNum2);
      this.setData({
        cai: randomNum2
      });

    let tip_arr=[];
      // 获取活动
    request.get(
      constans.getActivity,
      { activityId: wx.getStorageSync('activityId'), channel: wx.getStorageSync('channel'), invitation: wx.getStorageSync('in2') },
      function (data) {
        console.log(data)
        
        that.setData({
          tip: data.data.data.activity.extra.tip
        });
      })
  },
  // 点击牌子进行游戏
  tofan:function(index){
    var arry = this.data.tyle;
    console.log(index);
  
    if (arry.data.data2[index.currentTarget.dataset.index].a1!= 'none'){
  // 判断只有在这个定义的次数是3次之内下面代码才被执行、
    if(num<3){
      num_ci = num_ci+1;
    console.log(index.currentTarget.dataset.index);
    var inn = index.currentTarget.dataset.index
  
    console.log(arry)
    // 赋值点击中的进行反过来
    arry.data.data2[inn].a = 'transform: rotateY(-180deg) translateZ(0);';
      arry.data.data2[inn].a1 = 'none';
      arry.data.data2[inn].a2 = 'block';
      arry.data.data2[inn].a3 = 'transform: rotateY(0deg) translateZ(0);';
    this.setData({
      tyle: arry
    });
      let that = this
      var num1 = this.data.dip1;
      var num2 = this.data.dip2;
      var num3 = this.data.dip3;
    // 如果翻中红包的时候就显示出红包的弹出
      if (inn == this.data.cai) {
        that.setData({
          dis_d: 'block',
          img_user: wx.getStorageSync('avatar'),
        });
        setTimeout(function () {

          var time = setInterval(function () {
            num2 = num2 + 0.1;
            num1 = num1 + 0.1
            that.setData({
              dip2: num2,
              dip1: num1,
              dis1_d: 'block',
            });
           
            if (num2 > 1) {
              clearInterval(time)
              console.log(that.data.dip2 + num2);
            }
          }, 40)

        }, 500)
        num = num + 10;
      }
      num = num+1;
      //翻中三次没翻中
      if(num==3){
        that.setData({
          dis_d: 'block',
      
        });
        //延迟一秒让所有牌子翻开
        setTimeout(function () {
          var arry2 = that.data.tyle;
          for (var i = 0, len = arry2.data.data2.length;i<len;i++){

            arry2.data.data2[i].a = 'transform: rotateY(-180deg) translateZ(0);';
            arry2.data.data2[i].a1 = 'none'; 
            arry2.data.data2[i].a2 = 'block'; 
            arry2.data.data2[i].a3 = 'transform: rotateY(0deg) translateZ(0);';
          }
          that.setData({
            tyle: arry2
          });

          setTimeout(function () {

            var time = setInterval(function () {
              num2 = num2 + 0.1;
              num3 = num3 + 0.1
              that.setData({
                dip2: num2,
                dip3: num3,
                dis2_d: 'block',
              });
              console.log(that.data.dip2 + num2);
              if (num2 > 1) {
                console.log(that.data.dip2 + num2);
                clearInterval(time)
              }
            }, 40)

          }, 1000)
        },900);
       
      }
    }
    console.log('gas')
    }
  },
  // 点击刷新界面
  shuxin:function(){
    let that=this;
    console.log('agawg')
    num = 0;
    // wx.navigateTo({
    //   url: '../fGame/fGame'
    // })
    num_ju = num_ju+1;
    let num1 = 1;
    let num2 = 1;
    let num3 = 1;
    console.log(that.data)
    that.setData({
      tyle: {
        data: {
          data2: [
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);' },
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);' },
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);' },
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);' },
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);' },
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);' },
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);' },
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);' },
            { "a": 'transform: rotateY(0deg) translateZ(0);', "a1": 'block', "a2": 'none', "a3": 'transform: rotateY(-180deg) translateZ(0);' },
          ]
        }

      }
    });

    //延迟一秒让所有牌子翻开
 
    var time = setInterval(function () {
      num2 = num2 - 0.1;
      num3 = num3 - 0.1
      that.setData({
        dip2: num2,
        dip3: num3,
        dis2_d: 'none',
        dis_d:'none',
      });
      if (num2 < 0) {
        clearInterval(time)
      }
    }, 40)
    setTimeout(function () {
      let randomNum1 = Math.random() * 9;
      let randomNum2 = Math.floor(randomNum1);
      console.log(randomNum2);
      that.setData({
        cai: randomNum2
      });
    },200)
  },
  // 翻中牌领取
  hongbao:function(){
    let that=this;
    Toast.loading("领取中...");
    console.log(num_ju+';'+ num_ci);
 
     request.get(
      constans.grabPackage,
       { packageId: wx.getStorageSync('packageId'), channel: wx.getStorageSync('channel'), game: num_ju, times: num_ci, invitation:wx.getStorageSync('in2')},
      function (data) {
        if (data.data.code==0){
          var time_2 = setInterval(function () {
            request.get(
              constans.checkIsGrabPackage,
              { serial: data.data.data.serial },
              function (data1) {
                LogUtils.log(data1);
               
                if (data1.data.code == 0) {
                  wx.setStorageSync('qian', data1.data.data.amount);
                  clearInterval(time_2);
                  console.log('停');
                  wx.setStorageSync('yemianboll', 54)
                  wx.reLaunch({
                    url: '../packet/packet'
                  })
                }else{
                  Toast.hideLoading();
                  clearInterval(time_2);
                  wx.showModal({
                    title: '提示',
                    content: data1.data.message,
                    success:function(){
                  
                      wx.reLaunch({
                        url: '../packet/packet'
                      })
                    }
                  })
                }
               
              }
            )
          }, 3000)
        }else if(data.data.code==10002){
           wx.showModal({
              title: '提示',
             showCancel: false,
             content: data.data.message,
             success:function(){
               wx.reLaunch({
                 url: '../packet/packet'
               })
             }
            })
          
        }else{

        }
        
      }
    )
    that.setData({
      dip_bg: 'block'
    })
  }, onShareAppMessage: function (res) {
    let that=this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    request.get(
      constans.addShareRecord,
      { activityId: wx.getStorageSync('activityId'), packageId: wx.getStorageSync('packageId') },
      function (data) {

        if (data.data.code == 0) {
          console.log('分享成功')
          console.log(data)
          that.shuxin();
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: data.data.message,
          })
        }
      })
    return {
      title: wx.getStorageSync('title') + "邀您翻牌子",
      imageUrl: '../../img/Gran.jpg',
      path: "pages/packet/packet?c=share&a=" + wx.getStorageSync('activityId') + "&invitation=" + wx.getStorageSync('invitation') + "&timestamp=" + Date.parse(new Date()) + "&p=" + wx.getStorageSync('packageId')
 
    }
  },
  onShow(){
    var num_ci = 0;
    var num_ju = 1;

  },onReady(){

  }
 
})
 